package src.student;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;

public class TestStudent {

	public void studnet() throws FileNotFoundException, IOException {
		Scanner scanner = new Scanner(System.in);
		List<Student> studentlist = new ArrayList<Student>();

		Properties pero = new Properties();
		pero.load(new FileInputStream("studnet.txt"));
		Set<String> stringPropertyNames = pero.stringPropertyNames();
		for (String key : stringPropertyNames) {
			String property = pero.getProperty(key);
			String[] split = property.split(",");

			Student stu = new Student();
			stu.setName(split[0]);
			stu.setAge(split[1]);
			stu.setCalss(split[2]);
     
			studentlist.add(stu);
		}

		List<DomeClass> classlist = new ArrayList<DomeClass>();
		Properties perr = new Properties();
		perr.load(new FileInputStream("class.txt"));
		Set<String> stringPropertyNames2 = perr.stringPropertyNames();
		for (String key : stringPropertyNames2) {
			String value = perr.getProperty(key);
			String[] split = value.split(",");
			DomeClass dome = new DomeClass();
			dome.setDomeclass(split[0]);
			dome.setTeacher(split[1]);

			classlist.add(dome);
		}

		while (true) {

			System.out.println("1：新增，2：查找。3：删除，4：修改，5：退出");
			int i = scanner.nextInt();
			if (i == 1) {
				int b = 0;
				System.out.println("输入班级");
				String str = scanner.next();
				Iterator<DomeClass> iterator = classlist.iterator();
				while (iterator.hasNext()) {
					DomeClass domeClass = (DomeClass) iterator.next();
					if (str.equals(domeClass.getDomeclass())) {
						b++;
						Student studnet = new Student();
						studnet.setCalss(str);
						System.out.println("输入姓名");
						studnet.setName(scanner.next());
						String trmp = null;
						while (true) {
							System.out.println("输入年龄");
							 trmp = scanner.next();
							try {
								int parseInt = Integer.parseInt(trmp);
								break;
							} catch (Exception e) {
								System.out.println("请重新输入");
							}
						}
						studnet.setAge(trmp);

						studentlist.add(studnet);
					}
				}
				if (b == 0) {
					System.out.println("没有这个年级");
				}
			}
			if (i == 2) {
				Iterator<Student> iterator = studentlist.iterator();
				while (iterator.hasNext()) {
					Student student = (Student) iterator.next();
					System.out.println(student);
				}

			}
			if (i == 3) {
				System.out.println("输入你要删除的学生");
				String next = scanner.next();
				Iterator<Student> iterator = studentlist.iterator();
				Student stu = null;
				while (iterator.hasNext()) {
					Student student = (Student) iterator.next();
					if (student.getName().equals(next)) {
						stu = student;
					}
				}
				studentlist.remove(stu);
				System.out.println("删除成功");

			}
			if (i == 4) {
				System.out.println("输入你要修改的学生");
				String next = scanner.next();
				Iterator<Student> iterator = studentlist.iterator();
				while (iterator.hasNext()) {
					Student student = (Student) iterator.next();
					if (next.endsWith(student.getName())) {
						System.out.println("输入你要修改的姓名");
						student.setName(scanner.next());
					}
				}
			}
			if (i == 5) {
				Properties pro = new Properties();
				int s = 0;
				Iterator<Student> iterator = studentlist.iterator();
				while (iterator.hasNext()) {
					Student student = (Student) iterator.next();
					pro.setProperty("s" + s++, a(student));
				}
				System.out.println("在这里加了一行打印");
				pro.store(new FileOutputStream("studnet.txt"), "");
				break;
			}
			
		}
	}

	private String a(Student student) {
		System.out.println();
		return student.getName() + "," + student.getAge() + ","
				+ student.getCalss();
	}   
}
